package com.csylh.controller;

import com.csylh.Form.OrderForm;
import com.csylh.VO.ResponseVO;
import com.csylh.converter.OrderForm2OrderDTO;
import com.csylh.dto.OrderDTO;
import com.csylh.enums.ResultEnum;
import com.csylh.exception.OrderException;
import com.csylh.service.OrderService;
import com.csylh.service.UserOrderService;
import com.csylh.util.ResponseVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by  WuLei
 * Date 2018/3/7  22:04
 */
@RestController
@RequestMapping("/user/order")
@Slf4j
public class UserOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserOrderService userOrderService;
    /**
     * 新建订单
     * @param orderForm
     * @param bindingResult
     * @return 创建订单成功，返回订单ID
     */
    @PostMapping("/create")
    public ResponseVO create(@Valid OrderForm orderForm,
                             BindingResult bindingResult){
        log.info("orderForm={}",orderForm);
        //TODO 从jwt取出用户和收货员信息
        if(bindingResult.hasErrors()){
            log.info("【创建订单】参数不正确，orderForm={}",orderForm);
            throw new OrderException(ResultEnum.PARAM_ERROR,
                    bindingResult.getFieldError().getDefaultMessage());
        }
        //将orderForm转化为orderDTO
        OrderDTO orderDTO = OrderForm2OrderDTO.convert(orderForm);
//        System.out.println(orderDTO);
        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.info("【创建订单】购物车为空,orderDetailList={}",orderDTO.getOrderDetailList());
            throw new OrderException(ResultEnum.PARAM_ERROR);
        }
        String orderId = orderService.create(orderDTO);
        log.info("【创建订单】订单号={}",orderId);
        Map<String,String> map = new HashMap<>();
        map.put("orderId",orderId);
        return ResponseVOUtil.success(map);
    }

    //TODO 用户订单详情查询{用户ID+订单ID}
    @GetMapping("/detail")
    public ResponseVO<OrderDTO> detail(@RequestParam("userId") String userId,
                                     @RequestParam("orderId") String orderId) {
        OrderDTO orderDTO =  userOrderService.findOrderOne(userId, orderId);
        return ResponseVOUtil.success(orderDTO);
    }

    //TODO 查询个人订单列表
    //订单列表
    @GetMapping("/list")
    public ResponseVO<List<OrderDTO>> list(@RequestParam("userId") String userId,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(userId)) {
            log.error("【查询订单列表】userId为空");
            throw new OrderException(ResultEnum.PARAM_ERROR);
        }

        PageRequest request = new PageRequest(page, size);
        Page<OrderDTO> orderDTOPage = userOrderService.findList(userId, request);

        return ResponseVOUtil.success(orderDTOPage.getContent());
    }


    //TODO 取消订单
    @PostMapping("/cancel")
    public ResponseVO cancel(@RequestParam("userId") String userId,
                           @RequestParam("orderId") String orderId) {
        userOrderService.cancelOrder(userId, orderId);
        return ResponseVOUtil.success();
    }


}
